package com.pascal91.webpp.dao;

import com.pascal91.webpp.model.Users;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UsersDaoImpl implements UsersDao{

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<Users> getUsersList(){
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Users.class);
        return criteria.list();
    }

    @Override
    public void addUser(Users user) {
        sessionFactory.getCurrentSession().saveOrUpdate(user);
    }

    @Override
    public void deleteUserByID(Long id) {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Users.class);
        criteria.add(Restrictions.eq("id", id));
        Users user = (Users) criteria.uniqueResult();
        sessionFactory.getCurrentSession().delete(user);
    }

    @Override
    public Users findUserByLogin(String login) {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Users.class);
        criteria.add(Restrictions.eq("login", login));
        return (Users) criteria.uniqueResult();
    }

}
